<!--

/*#
# Copyright 2010 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code is not supported by Google
#
*/
-->

<!--
Sample .jsp code for Google Commerce Search to submit ShoppingAPI-Search queries 
 then parse out the responses for display. 
 
 Usage:
  
  Edit the file and specify the GCS instance ID (cx) and the API key you're 
  using.
  
  http://code.google.com/p/google-api-java-client/
  
  WEB-INF/lib/
   google-api-client-1.8.0-beta.jar
   google-http-client-1.8.3-beta.jar
   google-api-shopping-v1-rev7-java-1.5.0-beta.jar
   google-oauth-client-1.8.0-beta.jar
   guava-11.0.1.jar
   jackson-core-asl-1.9.4.jar
   
--->

<%@ page import='java.util.*' %>
<%@ page import='java.io.*' %>
<%@ page import='java.net.*' %>
<%@ page import='com.google.api.services.shopping.*' %>
<%@ page import='com.google.api.services.shopping.model.*' %>
<%@ page import='com.google.api.client.http.javanet.*' %>
<%@ page import='com.google.api.client.json.jackson.*' %>
<%@ page import='com.google.api.client.http.json.*' %>

<%@ page session="true" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
//Edit the following and enter in your API KEY and CX number
String devkey = "YOUR_API_KEY";
String cx = "YOUR_CX";  
String country = "us";
String language = "en";
%>

<html>
    <head>
    <title>GCS Search</title>

<!-- FOR UI -->  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <link rel="stylesheet" type="text/css" href="https://developers.google.com/css/screen.css" />
      <link href='//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>    

	<style type="text/css">
	body.docs table {
	    border-collapse: collapse !important; 
	    border: 0px solid #BBBBBB  !important;
	    border-spacing: 0  !important;
	    margin: 0 0 0em  !important;
	}
	</style>         
	
    </head>

<%   
 
   String q = request.getParameter("q");
   String html_results = "";
   String html_attributes = "";
   String totalResults = "";
   String userIP = request.getRemoteAddr();
   
   Shopping.Builder sb =Shopping.builder(new NetHttpTransport(), new JacksonFactory());
   
   Shopping shopping = sb.build();
   
          try {
                Shopping.Products.List req = shopping.products().list("cx:" + cx)
               .setQ(q)
               .setCountry(country)
               .setLanguage(language)
               .setStartIndex(1L)
               .setMaxResults(10L)
               .setSafe(false)
               .setSpellingEnabled(true)
               .setSpellingUseGcsConfig(true)
               .setPromotionsEnabled(true)
               .setPromotionsUseGcsConfig(true)
               .setRedirectsEnabled(true)
               .setRedirectsUseGcsConfig(true)
               .setFacetsEnabled(true)
               .setFacetsUseGcsConfig(true)
               .setFacetsDiscover("10:10")
               .setAvailability("outOfStock,limited,inStock");
               //.setMaxVariants(5);              
               req.setUserIp(userIP);
               req.setKey(devkey);
                              
               Products ps = req.execute();
               
               totalResults = Integer.toString(ps.getTotalItems());
			   if (Integer.parseInt(totalResults) > 0)
			   {
	               for (Product p : ps.getItems() ){
	            	   html_results = html_results + "<tr><td><strong><a href='" + p.getProduct().getLink() + "'>" + p.getProduct().getTitle() + "</a></strong><br/>";
	            	   
	            	   if (p.getProduct().getImages() != null)
		            	   for (ShoppingModelProductJsonV1.Images i : p.getProduct().getImages()){
							   html_results = html_results + "<img src ='" + i.getLink() + "'/><br/>";
	        	    	   }
	            	   
	            	   if (p.getProduct().getInventories() != null)
		            	   for (ShoppingModelProductJsonV1.Inventories i : p.getProduct().getInventories()){
							   html_results = html_results + "&nbsp;&nbsp;&nbsp;&nbsp;" + i.getCurrency() + " " + i.getPrice() + "<br/>";
	        	    	   }  
	            	   
	            	   if (p.getProduct().getAttributes() != null)
		            	   for (ShoppingModelProductJsonV1.Attributes a : p.getProduct().getAttributes()){
							   html_results = html_results + "&nbsp;&nbsp;&nbsp;&nbsp;" + a.toPrettyString() + "<br/>";
	        	    	   }  
	            	   
	            	   if (p.getProduct().getVariants() != null)
	            	   {
	            		   html_results = html_results + "<em>Variants:</em><table>";
		            	   for (ShoppingModelProductJsonV1.Variants v : p.getProduct().getVariants()){
							   html_results = html_results + "<tr><td>" + v.getVariant().getTitle()  + "</td><td>" + v.getVariant().getProvidedId() + "</td></tr>";
	        	    	   }   
	            		   html_results = html_results + "</table>";
	            	   }
	            	   
	            	   html_results =  html_results + "</td></tr>";
	               }
	               
	               for (Products.Facets f : ps.getFacets() ){
	            	   html_attributes = html_attributes + "<tr><td><strong>" + f.getDisplayName() + "</strong><br/>";
	            	   if (f.getBuckets() != null)
	            	     for (Products.Facets.Buckets b : f.getBuckets()) {
	            		     html_attributes = html_attributes +  "&nbsp;&nbsp;&nbsp;&nbsp;" +  b.toPrettyString() + "<br/>"; 
	            	     }
					   html_attributes = html_attributes + "</td></tr>";            	               	  
	               }   
			   }
               
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
       }          	        
%>


<body class="two-column docs">

    <div id="gc-wrapper">
    <div id="gc-main">
    <div id="gc-content">
    
	    <center>
		<img src="http://www.google.com/intl/en/images/logos/commerce_search_logo.gif" alt="Google Commerce Search"/>
		    <form id="searchForm" method="GET">
			    <table>
		    		<tr><td>search:</td><td><input id="q" name="q" type="text" size="45%" autocomplete="off" /></td></tr>     
				</table>	    			    
				<input type="submit" value="Submit" "/>	
		    </form>	
		</center>
		
		<em>Total Results:  <%=totalResults%></em>
		<hr/>
		<table width="100%" border=2>
		<tr align="top" >
		  <td width="25%" id="attributes" name="attributes"  valign="top"><b><u>Attributes</u></b><table><%=html_attributes%></table></td>
		  <td width="75%" id="results" name="results"  valign="top" ><b><u>Results</u></b><table><%=html_results%></table></td>
		</tr>
		</table>

	</div>
	</div>
	</div>
</body>
</html>

